﻿{*******************************************************}
{  修改秋风大神的作品:DataSetDelta                      }
{ 为delphi unidac 增加类似TClientDataSet的Delta功能     }
{  至此:该控件支持delphi lazarus                        }
{  可按此方法修改内存表为friedac                        }
{  modify by ccc(QQ1650680975)                          }
{ https://gitee.com/cityboat888/DataSetDelta.git        }
{*******************************************************}
unit unit_D;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB,
   UniProvider, SQLiteUniProvider,DBAccess, Uni, MemDS, VirtualTable,DataSetDelta,
    Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids;

type
  TForm6 = class(TForm)
    DBGrid1: TDBGrid;
    Memo1: TMemo;
    Button2: TButton;
    DBGrid2: TDBGrid;
    Button3: TButton;
    btnMon: TButton;
    combTable: TComboBox;
    btnConn: TButton;
    DataSource1: TDataSource;
    VirtulTable1: TVirtualTable;
    DataSource2: TDataSource;
    UniConnection1: TUniConnection;
    UniQuery1: TUniQuery;
    SQLiteUniProvider1: TSQLiteUniProvider;
    btnOpen: TButton;
    btnCover: TButton;
    btnUadd: TButton;
    btnVedit: TButton;
    btnVadd: TButton;
    btnVdel: TButton;
    btnUedit: TButton;
    btnUdel: TButton;
    procedure btnConnClick(Sender: TObject);
    procedure btnMonClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure btnUaddClick(Sender: TObject);
    procedure btnVeditClick(Sender: TObject);
    procedure btnUeditClick(Sender: TObject);
    procedure btnUdelClick(Sender: TObject);
    procedure btnVdelClick(Sender: TObject);
    procedure btnVaddClick(Sender: TObject);
  private
    { Private declarations }
  public
    dcm1:TQFDataSetMonitor;
    dcm2:TQFDataSetMonitor;
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}


procedure TForm6.FormCreate(Sender: TObject);
begin
  dcm1:=TQFDataSetMonitor.Create(nil);
  dcm1.DataSet:=UniQuery1;
  dcm2:=TQFDataSetMonitor.Create(nil);
  dcm2.DataSet:=VirtulTable1;
end;

procedure TForm6.FormDestroy(Sender: TObject);
begin
  if Assigned(dcm1) then freeandnil(dcm1);
  if Assigned(dcm2) then freeandnil(dcm2);
end;

procedure TForm6.btnMonClick(Sender: TObject);
begin
  if dcm1.Active then
  begin
    dcm1.Active:=false;
    dcm2.Active:=false;
    btnMon.caption:='开启监控';
  end
  else
  begin
    dcm1.Active:=true;
    dcm2.Active:=true;
    btnMon.caption:='停止监控';
  end;
end;

procedure TForm6.Button2Click(Sender: TObject);
var
  sql:string;
begin
  if UniQuery1.State in [dsEdit, dsInsert] then
    UniQuery1.Post;
  sql:=dcm1.GetActionSQL('table1');
  if sql<>'' then
    memo1.Lines.Add(sql);
end;

procedure TForm6.Button3Click(Sender: TObject);
var sql:string;
begin
  if VirtulTable1.State in [dsEdit, dsInsert] then
    VirtulTable1.Post;
  sql:=dcm2.GetActionSQL('table2');
  if sql<>'' then
    memo1.Lines.Add(sql);
end;

procedure TForm6.btnConnClick(Sender: TObject);
begin
  UniConnection1.Disconnect;
  {$IFDEF FPC}
    {$ifdef linux}
    SQLiteLibraryName:=ExtractFilePath(Application.ExeName)+'libsqlite3.so';
    {$else}
    SQLiteLibraryName:=utf8tocp936(ExtractFilePath(Application.ExeName)+'sqlite3.dll');
    {$endif}
    UniConnection1.Protocol:='sqlite';
    UniConnection1.LibraryLocation:=utf8tocp936(ExtractFilePath(Application.ExeName)+{$ifdef linux}'libsqlite3.so'{$else}'sqlite3.dll'{$endif});
    UniConnection1.Properties.Clear;
    UniConnection1.Properties.Add('encrypted=yes');
    UniConnection1.Properties.Add('cipher=sqlcipher');
    UniConnection1.Properties.Add('sqlcipher=legacy');
    UniConnection1.Properties.Add('legacy=1');
    UniConnection1.Properties.Add('controls_cp=CP_UTF8');
    UniConnection1.Properties.Add('AutoEncodeStrings=True');
//  UniConnection1.Password:='123asd';
   {$ELSE}
    UniConnection1.ProviderName:='SQLite';
    UniConnection1.Database:='..\..\config.db';

   {$ENDIF}

  UniConnection1.Connect;
  UniConnection1.GetTableNames(combTable.Items);
  combTable.ItemIndex:=0;
  //QFDataSetMonitor1.Active:=true;

end;

procedure TForm6.btnOpenClick(Sender: TObject);
var
  lstream:TMemoryStream;
begin
  UniQuery1.Close;
  UniQuery1.SQL.Text:='select * from '+combTable.Text;
  UniQuery1.Open;

  lstream:=TMemoryStream.Create;
  try
    UniQuery1.SaveToXML(lstream);
    lstream.Position:=0;
    VirtulTable1.LoadFromStream(lstream);
    VirtulTable1.Open;
  finally
    lstream.Free;
  end;

  if not dcm1.Active then
  begin
    btnMonClick(Sender);
  end ;
end;

procedure TForm6.btnUaddClick(Sender: TObject);
begin
   UniQuery1.Append;
   UniQuery1.FieldByName('name').AsString:='001add';
   UniQuery1.FieldByName('sq').AsString:='test';
   UniQuery1.Post;


end;


procedure TForm6.btnUdelClick(Sender: TObject);
begin
  UniQuery1.First;
  UniQuery1.Next;
  UniQuery1.Delete;
end;

procedure TForm6.btnUeditClick(Sender: TObject);
begin
   UniQuery1.First;
   UniQuery1.Edit;
   UniQuery1.FieldByName('name').AsString:='001edit';
   UniQuery1.FieldByName('sq').AsString:='test';
   UniQuery1.Post;
end;





procedure TForm6.btnVaddClick(Sender: TObject);
begin
   VirtulTable1.Append;
   VirtulTable1.FieldByName('name').AsString:='001add';
   VirtulTable1.FieldByName('sq').AsString:='test';
   VirtulTable1.Post;
end;


procedure TForm6.btnVeditClick(Sender: TObject);
begin
   VirtulTable1.First;
   VirtulTable1.Edit;
   VirtulTable1.FieldByName('name').AsString:='001edit';
   VirtulTable1.FieldByName('sq').AsString:='test';
   VirtulTable1.Post;

end;

procedure TForm6.btnVdelClick(Sender: TObject);
begin

  VirtulTable1.First;
  VirtulTable1.Next;
  VirtulTable1.Delete;
end;

end.
